/*
 * $Id: BaseRepository.java 42 2013-04-17 15:33:59Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 42 $
 * $Date: 2013-04-17 15:33:59 +0000 (Wed, 17 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.data.repository;

import com.sci.logisticsmap.support.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
import org.springframework.data.repository.NoRepositoryBean;

import java.io.Serializable;

/**
 * The Repository base class for accessing model.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 42 $
 */
@NoRepositoryBean
//public interface BaseRepository<Model extends Serializable, ID extends Serializable> extends MongoRepository<Model, ID> {
public abstract class BaseRepository<Model extends Serializable, ID extends Serializable> extends SimpleMongoRepository<Model, ID> {
    /**
     * The logger.
     */
	protected Logger logger;

    /**
     * Initializes a Repository base class.
     *
     * @param metadata The MongoDB specific model information.
     * @param mongoOperations The MongoDB operations.
     */
    public BaseRepository(MongoEntityInformation<Model, ID> metadata, MongoOperations mongoOperations) {
        super(metadata, mongoOperations);

        logger = LoggerFactory.getLogger(getClass());
    }

    /**
     * Gets the model page.
     *
     * @param page The page number.
     * @param pageSize The page size.
     *
     * @return The model page.
     */
	public Page<Model> findAll(int page, int pageSize) {
		return findAll(page, pageSize, null);
	}

    /**
     * Gets the model page.
     *
     * @param page The page number.
     * @param pageSize The page size.
     * @param direction The sorting direction.
     * @param properties The sorting properties.
     *
     * @return The model page.
     */
	public Page<Model> findAll(int page, int pageSize, Sort.Direction direction, String... properties) {
		return findAll(page, pageSize, new Sort(direction, properties));
	}

    /**
     * Gets the model page.
     *
     * @param page The page number.
     * @param pageSize The page size.
     * @param sort The sorting option.
     *
     * @return The model page.
     */
	public Page<Model> findAll(int page, int pageSize, Sort sort) {
        org.springframework.data.domain.Page<Model> modelPage = findAll(new PageRequest(page - 1, pageSize, sort));
        if (modelPage != null) {
            return convertPage(modelPage);
        }
        return null;
	}

    /**
     * Converts the Spring Data domain page to the model page.
     *
     * @param sourcePage The Spring Data domain page.
     *
     * @return The model page.
     */
    private <Source> Page<Source> convertPage(org.springframework.data.domain.Page<Source> sourcePage) {
        if (sourcePage != null) {
            return new Page<Source>(sourcePage.getContent(), sourcePage.getNumber() + 1, sourcePage.getSize(), sourcePage.getTotalElements());
        }
        return null;
    }
}
